//
//  HMEmoticonToolBar.swift
//  swift-新浪微博0
//
//  Created by 赵玉忠 on 16/9/9.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit


/// 表情的类型
///
/// - RECENT:  最近
/// - DEFAULT: 默认
/// - EMOJI:   emoji
/// - LXH:     浪小花
enum HMEmotionType: Int {
    case RECENT = 0, DEFAULT, EMOJI, LXH
}

class HMEmoticonToolBar: UIView {
    
    var currentSelectedButton: UIButton?
    
    var selectedIndexPath: IndexPath? {
        didSet {
            // 找到indexPath中的section对应的按钮，选中该按钮
            // 取到指定的按钮
            let button = self.stackView.viewWithTag(selectedIndexPath!.section)! as! UIButton
            
            // 判断当前点击的按钮与选中的按钮是同一个按钮的话，就不执行后面的代码
            if currentSelectedButton == button {
                return
            }
            
            // 1. 让之前选中的按钮取消选中
            currentSelectedButton?.isSelected = false
            
            // 2. 让当前点击的按钮选中
            button.isSelected = true
            
            // 3. 将当前选中的的按钮记录起来，记录起来原因是下次在点击其他按钮的时候，要取消当前按钮
            currentSelectedButton = button
            
        }
    }
    
    /// 表情类型切换按钮点击的时候执行的闭包
    var emotionTypeChangedClosure: ((HMEmotionType)->())?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI(){
        // 将自己的tag设置成1000
        addSubview(stackView)
        
        stackView.snp_makeConstraints { (make) in
            make.edges.equalTo(self)
        }
        
        addChildButton(title: "最近", bgImgNamed: "compose_emotion_table_left", type: .RECENT)
        addChildButton(title: "默认", bgImgNamed: "compose_emotion_table_mid", type: .DEFAULT)
        addChildButton(title: "Emoji", bgImgNamed: "compose_emotion_table_mid", type: .EMOJI)
        addChildButton(title: "浪小花", bgImgNamed: "compose_emotion_table_right", type: .LXH)
    }
    
    private func addChildButton(title: String, bgImgNamed: String, type: HMEmotionType) {
        let button = UIButton()
        
        button.addTarget(self, action: #selector(childButtonClick(button:)), for: UIControlEvents.touchUpInside)
        
        // 记录当前传入的表情类型
        button.tag = type.rawValue
        
        button.setTitle(title, for: UIControlState.normal)
        // 设置字体大小
        button.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        // 设置不同状态下的文字颜色
        button.setTitleColor(UIColor.white, for: UIControlState.normal)
        button.setTitleColor(UIColor.gray, for: UIControlState.selected)
        
        // 设置不同状态下的背景图片
        button.setBackgroundImage(UIImage(named: "\(bgImgNamed)_normal"), for: UIControlState.normal)
        button.setBackgroundImage(UIImage(named: "\(bgImgNamed)_selected"), for: UIControlState.selected)
        
        stackView.addArrangedSubview(button)
    }
    
    // MARK: - 监听事件
    
    @objc private func childButtonClick(button: UIButton) {
        
        // 判断当前点击的按钮与选中的按钮是同一个按钮的话，就不执行后面的代码
        if currentSelectedButton == button {
            return
        }
        
        // 1. 让之前选中的按钮取消选中
        currentSelectedButton?.isSelected = false
        
        // 2. 让当前点击的按钮选中
        button.isSelected = true
        
        // 3. 将当前选中的的按钮记录起来，记录起来原因是下次在点击其他按钮的时候，要取消当前按钮
        currentSelectedButton = button
        
        // 4. 在这个地方通知外界去滚动collectionView
        // 也就是说需要在此执行一个闭包
        emotionTypeChangedClosure?(HMEmotionType(rawValue: button.tag)!)
    }
    
    
    // MARK: - 懒加载事件
    
    private lazy var stackView: UIStackView = {
        // FDStackView
        let v = UIStackView()
        v.tag = 1000
        // 设置布局方向
        // v.axis =
        v.distribution = .fillEqually
        return v
    }()
}
